package com.logic.modular.gameCore.pay.drawCards.service.impl;

import com.logic.modular.gameCore.pay.drawCards.mapper.CoreDrawPoolMapper;
import com.logic.modular.gameCore.pay.drawCards.service.CoreDrawPoolService;
import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comment.pojo.domain.logic.core.CoreDrawPool;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;


/**
 * (CoreDrawPool)表服务实现类
 *
 * @author cxy
 * @since 2024-08-07 16:22:01
 */
@Slf4j
@Service
public class CoreDrawPoolServiceImpl extends ServiceImpl<CoreDrawPoolMapper, CoreDrawPool> implements CoreDrawPoolService {
    Cache<String, Map<Long, List<CoreDrawPool>>> cache = Caffeine.newBuilder().build();

    @PostConstruct
    private void init() {
        CoreDrawPool initSelect = CoreDrawPool.builder().build();
        List<CoreDrawPool> list = this.list(new LambdaQueryWrapper<>(initSelect));
        log.info("开始缓存数据,数据量为:{}", list.size());
        cache.put("coreDrawPool", list.stream().collect(Collectors.groupingBy(CoreDrawPool::getPoolId)));
        log.info("数据缓存完成");
    }

    public List<CoreDrawPool> listCache() {
        Map<Long, List<CoreDrawPool>> values = mapCache();
        return values.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
    }

    public List<CoreDrawPool> listCache(Long poolId) {
        List<CoreDrawPool> coreDrawPools = mapCache().get(poolId);
        if (coreDrawPools==null||coreDrawPools.isEmpty()){
            throw new RuntimeException("数据为空");
        }
        return coreDrawPools;
    }

    public Map<Long, List<CoreDrawPool>> mapCache() {
        Map<Long, List<CoreDrawPool>> values = cache.getIfPresent("coreDrawPool");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values;
    }

    public CoreDrawPool getCache(Long poolId, Long rewardId) {
        Map<Long, List<CoreDrawPool>> values = mapCache();
        return values.get(poolId).stream().filter(e -> e.getRewardId().equals(rewardId)).findFirst().orElse(null);
    }

    public CoreDrawPool draw(Long poolId) {
        List<CoreDrawPool> coreDrawPools = listCache(poolId);
        // 获取权重总和
        long weight = 0;
        for (CoreDrawPool coreDrawPool : coreDrawPools) {
            weight += coreDrawPool.getWeight();
        }
        // 随机数
        long index = ThreadLocalRandom.current().nextLong(0L, weight + 1);
        for (CoreDrawPool coreDrawPool : coreDrawPools) {
            if (index < coreDrawPool.getWeight()){
                return coreDrawPool;
            }else {
                index -= coreDrawPool.getWeight();
            }
        }
        return null;
    }
}
